package com.gxwy.yonghuziliao.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gxwy.yonghuziliao.form.YongHuZiLiao;
import com.gxwy.yonghuziliao.service.YongHuZiLiaoService;

@Controller
@RequestMapping("/yonghuziliao")
public class YongHuZilLaoController{


		@Autowired
		private YongHuZiLiaoService contactService; 

		@RequestMapping("/lists")
		public String listContacts(Map<String, Object> map,HttpSession session) {
            String username=(String) session.getAttribute("username");
			map.put("contact", new YongHuZiLiao());
			map.put("contactList", contactService.findAll(username));
	      
		  
			return "yonghuziliao";
		}

		@RequestMapping(value = "/addC", method = RequestMethod.POST)
		public String addContact(@ModelAttribute("contact")
		YongHuZiLiao contact, BindingResult result,HttpSession session) {
	        
			
			contactService.addYongHuZiLiao(contact);

	        return "redirect:lists";
		
		}
		
		@RequestMapping(value = "/update", method = RequestMethod.POST)
		public String update(@ModelAttribute("contact")
		YongHuZiLiao contact, BindingResult result,String name,Map<String, Object> map)
		{

			if(name==""){
				 return "redirect:lists";
			}
			contactService.Update(contact,Integer.valueOf(name));
		
			 return "redirect:lists";

			
		}

		@RequestMapping(value = "/delete1",method = RequestMethod.POST)
		public String deleteContact(String delet) {
			if(delet==""){
		    return "redirect:lists";
				
			}
			contactService.Delete(Integer.valueOf(delet));

			return  "redirect:lists";
		}
	
}
	

